home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
C and C++
/
Science⁄Math
/
Scientist's Helper src
/
s.helper.5
/
regtable.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-02-06
|
7KB
|
291 lines
#include "all.h"
#include "regtabext.h"
InitTable() /*to be called once at beginning of program*/
{
int i, j;
for (i=0; i<32; i++ ) {
table.ptr[i] = NULL;
} /*end for*/
strcpy(table.header.title,"");
table.header.samp = 1.0;
table.header.start = 0.0;
table.header.interpolated = FALSE;
AllocTable( 128, 2 );
strcpy(table.header.title,"Untitled Table");
table.header.samp = 1.0;
table.header.start = 0.0;
table.header.interpolated = FALSE;
Header2Vars();
}
AllocTable( r, c ) /*allocates table on heap*/
int r, c;
{
int i,j, blocks, error, status;
char str[32];
if ( (r<1) || (r>4096) ) {
ErrMsg("rows out of range 1-4096");
}
if ( (c<1) || (c>32) ) {
ErrMsg("cols out of range 1-32");
}
for (i=0; i<32; i++) { /*release memory used by previous table*/
if( table.ptr[i] != NULL ) {
DisposHandle( table.ptr[i] );
table.ptr[i] = NULL;
} /*end if*/
} /*end for*/
error = FALSE;
for ( i=0; i<c; i++ ) { /*capture new memory*/
table.ptr[i] = NewHandle((long)r*4L);
if (MemError()!=noErr){
error = TRUE;
break;
}/*end if*/
}/*for*/
if( !error ) {
table.header.rows = r;
table.header.cols = c;
table.header.maxRows = r;
table.header.maxCols = c;
for ( i=1; i<=c; i++ ) { /*initialize column names*/
IToS( i, str );
strcpy( table.header.colName[i-1], "Col " );
strcat( table.header.colName[i-1], str);
} /*end for */
for ( i=1; i<=r; i++ ) { /*initialize table values*/
for ( j=1; j<=c; j++ ) {
SetTable( i, j, infinity, TRUE );
} } /*end fors*/
CreateCol1();
Header2Vars();
WritePhrase("new table ");
IToS( r, str );
WritePhrase(str);
WritePhrase(" by ");
IToS( c, str );
WriteLine( str );
}
else if ( error && (r==128) && (c==2) ) {
WriteLine("Fatal error. no space for minimum table");
doneFlag = TRUE;
}
else {
ErrMsg("not enough space. Try a smaller table");
AllocTable( 128, 2 );
}
}
SetTable( r, c, value, override)
int r, c, override;
float value;
/* sets table to given value unless entry in row 1 and table is interpolated*/
/* override=TRUE overrides this exception*/
{
int i,j, block, offset;
char str[80];
if( (r<1) || (r>table.header.rows) ) {
IToS( r, str );
strcat( str, " cant set row outside table" );
ErrMsg(str);
}
else if( (c<1) || (c>table.header.cols) ) {
IToS( c, str );
strcat( str, " cant set col outside table" );
ErrMsg(str);
}
else if( (table.header.interpolated) && (c==1) && (!override) ) {
ErrMsg("cant change col 1 of interpolated table");
}
(**table.ptr[c-1])[r-1] = value;
}
GetTable( r, c, f ) /*gets table entry*/
int r, c;
float *f;
{
int i,j, block, offset;
char str[80];
if ( (r<1) || (r>table.header.rows) ) {
IToS( r, str );
strcat( str, " cant get row outside table");
ErrMsg(str);
}
if ( (c<1) || (c>table.header.cols) ) {
IToS( c, str );
strcat( str, " cant get col outside table");
ErrMsg(str);
}
*f = (**table.ptr[c-1])[r-1];
}
ReadTable(reply)
SFReply *reply;
{
char str[256];
int i, j, k, firstCol, f, oldMaxRows, oldMaxCols;
long regTabType, count, bytes;
Finfo info;
if( !(reply->good) ) {
return;
}
strncpy( (char*)(®TabType), "RGTB", 4 );
oldMaxRows = table.header.maxRows;
oldMaxCols = table.header.maxCols;
if( GetFInfo( reply->fName, reply->vRefNum, &info )!=noErr ) {
ErrMsg("couldnt get file info");
}
if( info.fdType!=regTabType ) {
ErrMsg("not a RegTab file");
}
/*open file*/
if( FSOpen( reply->fName, reply->vRefNum, &f)!=noErr ) {
ErrMsg("couldnt open file");
}
SetFPos( f, fsFromStart, 0L );
count=512L;
k=FSRead(f,&count,&(table.header));
if( (k!=noErr) || (count!=512L) ) {
FSClose(f);
ErrMsg("couldnt read header");
}
if ( (table.header.rows<1) || (table.header.rows>4096)
|| (table.header.cols<1) || (table.header.cols>32) ) {
FSClose(f);
ErrMsg("table header is incorrect");
}
/* case more space is needed, allocate it and try again*/
if( (oldMaxRows<(table.header.rows)) || (oldMaxCols<table.header.cols) ) {
FSClose(f);
AllocTable( table.header.rows, table.header.cols );
WriteLine("Warning: table allocation changed");
ReadTable(reply);
}
else {
table.header.maxCols = oldMaxCols;
table.header.maxRows = oldMaxRows;
for( i=(table.header.cols+1); i<=table.header.maxCols; i++ ) {
strcpy( table.header.colName[i-1], "Col " );
IToS( i, str );
strcat( table.header.colName[i-1], str );
}
if (table.header.interpolated) {
firstCol=1;
}
else {
firstCol=0;
}
bytes = (long)table.header.rows*4L;
for( j=firstCol; j<table.header.cols; j++ ) {
HLock(table.ptr[j]);
count = bytes;
k=FSRead(f,&count,*(table.ptr[j]));
HUnlock(table.ptr[j]);
if( (k!=noErr) || (count!=bytes) ) {
FSClose(f);
ErrMsg("couldnt read table");
}
} /*end for*/
FSClose(f);
CreateCol1();
RedoEditWindow();
Header2Vars();
Header2Vars();
}
}
WriteTable(reply)
SFReply *reply;
{
int f, i, j, k, firstCol;
char str[256];
long count, bytes, bytesNeeded, bytesAvailable, regTabType, t;
Finfo info;
if( !(reply->good) ) {
return;
}
strncpy( ®TabType, "RGTB", 4 );
if( GetFInfo( reply->fName, reply->vRefNum, &info )==noErr ) { /*delete old file, if any*/
if( info.fdType!=regTabType ) {
ErrMsg("cant overwrite a non-RegTab file");
}
else {
if( FSDelete(reply->fName, reply->vRefNum)!=noErr ) {
ErrMsg("couldnt delete existing file");
}
}
}
if (table.header.interpolated) {
firstCol=1;
}
else {
firstCol=0;
}
bytesNeeded = 512L + ( 4L * (long)table.header.rows * (long)(table.header.cols-firstCol) );
bytesAvailable = 0;
for( i=0; i<10; i++ ) {
k=GetVInfo( i, str, &j, &count );
if( (k==noErr) && (j==reply->vRefNum) ) {
bytesAvailable=count;
break;
}
}
if( bytesAvailable<bytesNeeded ) {
ErrMsg( "not enough space on disk" );
}
if( Create(reply->fName, reply->vRefNum, regTabType, regTabType)!=noErr ) {
ErrMsg("couldnt create file");
}
if( FSOpen(reply->fName, reply->vRefNum, &f )!=noErr ) {
ErrMsg("couldnt open file");
}
if( SetFPos( f, fsFromStart, 0L )!=noErr ) {
ErrMsg("couldnt find beginning of file");
}
count=512L;
k = FSWrite(f, &count, &(table.header) );
if( (k!=noErr) || (count!=512L) ) {
FSClose(f);
ErrMsg("couldnt write header");
}
bytes = (long)table.header.rows * 4L;
for( j=firstCol; j<table.header.cols; j++) {
HLock(table.ptr[j]);
count=bytes;
k =FSWrite(f, &count, *(table.ptr[j]) );
HUnlock(table.ptr[j]);
if( (k!=noErr) || (count!=bytes) ) {
FSClose(f);
ErrMsg("couldnt write table");
}
} /*end fors*/
if( FSClose(f)!=noErr ) {
ErrMsg("couldnt close file");
}
FlushVol( str, reply->vRefNum );
}